/**
 * Baijiahulian.com Inc. Copyright (c) 2014-2014 All Rights Reserved.
 */
package com.baijia.dao.impl;

import com.baijia.dao.OrgAccountDao;
import com.baijia.dao.po.OrgAccount;

import com.mysql.jdbc.Statement;

import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.PreparedStatementCreator;
import org.springframework.jdbc.support.GeneratedKeyHolder;
import org.springframework.jdbc.support.KeyHolder;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;

/**
 * @title OrgAccountDaoImpl
 * @desc 机构账户dao实现类
 * @author caoliang
 * @date 2015年12月5日
 * @version 1.0
 */
@Repository
@Transactional
public class OrgAccountDaoImpl extends CommonDaoImpl<OrgAccount, Integer> implements OrgAccountDao {

    public OrgAccountDaoImpl() {
        super(OrgAccount.class);
    }

    @Override
    public OrgAccount findByMobile(String mobile, String countryCode) {
        try {
            return jdbcTemplate.queryForObject(selectSql + " where mobile=?", new BeanPropertyRowMapper<>(
                OrgAccount.class), new Object[] { mobile });
        } catch (Exception e) {
            return null;
        }
    }

    private static String INSERT_ORG_USER =
        "insert into cdb.org_account(mobile,password,username,email,number,mobile_verify,created_at) "
            + "values(?,?,?,?,0,1,now())";

    @Override
    public long save(final OrgAccount orgAccount) {
        KeyHolder keyHolder = new GeneratedKeyHolder();
        jdbcTemplate.update(new PreparedStatementCreator() {
            @Override
            public PreparedStatement createPreparedStatement(Connection con) throws SQLException {
                PreparedStatement ps = con.prepareStatement(INSERT_ORG_USER, Statement.RETURN_GENERATED_KEYS);
                int index = 0;
                ps.setString(++index, orgAccount.getMobile());
                ps.setString(++index, orgAccount.getPassword());
                ps.setString(++index, orgAccount.getUserName());
                ps.setString(++index, orgAccount.getEmail());
                return ps;
            }
        }, keyHolder);

        long autoIncId = keyHolder.getKey().longValue();
        return 0;
    }

    @Override
    public int updateNumber(long lastInsertId, long number) {
        return 0;
    }

    private String selectSql =
        "select id, number, username as userName, password, mobile, mobile_verify_status, createtime as createTime, updatetime as updateTime, isdel as isDel, modifytime as modifyTime,country_code, symbol from yunying.org_account ";

    @Override
    protected String selectSql() {
        return selectSql;
    }

    @Override
    public Long getIdByNumber(Long number) {
        return jdbcTemplate.queryForObject("select id from yunying.org_account where number=?",
            new Object[] { number }, Long.class);

    }

    @Override
    public int updateMobileById(String countrySymbol, String countryCode, String mobile, Long orgId) {
        return jdbcTemplate.update(
            "update yunying.org_account set symbol=?, country_code=?, mobile=?, mobile_verify_status=1 where id=?",
            new Object[] { countrySymbol, countryCode, mobile, orgId });
    }

}